﻿using BLL.TableBLL;
using Models.TableModel;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tools;
using webui.Models;

namespace webui.Areas.Admin.Controllers
{
    public class UserInfoController : Controller
    {
        // GET: Admin/UserInfo
        [SkipPemission]
        public ActionResult Index()
        {
           
            if (OperateContext.Current.IsLogin())
            {

                BLL.UserInfoBLL ubll = new BLL.UserInfoBLL();
              var umodle=   ubll.GetSingle(s => s.loginname == OperateContext.Current.Usr.loginname);
                OperateContext.Current.Usr = umodle;
                ViewBag.username = OperateContext.Current.Usr.username;
                ViewBag.loginname = OperateContext.Current.Usr.loginname;
                ViewBag.downNum = umodle.downNum;
                ViewBag.memberType = OperateContext.Current.Usr.memberType;
                return View();
            }
            else
            {
                return OperateContext.Current.RedirectAjax("ok", " 请登录", null, "/admin/admin/Login");
            }
          
        }


        [HttpPost]
        /// <summary>
        /// 下载管理 视图
        /// </summary>
        /// <returns></returns>
        public ActionResult UserInfoList()
        {
            DownLogBLL downbll = new DownLogBLL();
            //获取页容量
            int pageSize = int.Parse(Request.Form["rows"]);
            //获取请求的页码
            int pageIndex = int.Parse(Request.Form["page"]);
             
            string keyword = Request.Form["keyword"];

           

            // 查询分页数据
            PageModel page = new PageModel()
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                PageCount = 0
            };

            List<DownLog> list = new List<DownLog>();

          
            if (OperateContext.Current.Usr.loginname== "admin")
            {
                if (!string.IsNullOrEmpty(keyword))
                {
                    list = downbll.GetPageList(p => 1 == 1, page);
                }
                else
                {
                    list = downbll.GetPageList(p => p.pname.Contains(keyword), page);
                }
              
            }
            else
            {
                if (!string.IsNullOrEmpty(keyword))
                {
                    list = downbll.GetPageList(p => p.userid == OperateContext.Current.Usr.id && p.pname.Contains(keyword), page);
                }
                else
                {
                    list = downbll.GetPageList(p => p.userid == OperateContext.Current.Usr.id, page);
                }

               
            }

            //2 生成规定格式的 json字符串发回 给 异步对象
            DataGridModel dgModel = new DataGridModel()
            {
                total = page.PageCount,
                rows = list,
                footer = null
            };
            return Json(dgModel);
        }

        [SkipPemission]
        public ActionResult DownloadFile(int downid)
        {
            if (OperateContext.Current.IsLogin() ==false)
            {
                return OperateContext.Current.RedirectAjax("ok", " 请登录", null, "/admin/admin/Login");
            }
           

            DownLogBLL downbll = new DownLogBLL();
            DownLog oneModel= downbll.GetSingle(s => s.id == downid);
            if (oneModel!=null)
            {
                if (oneModel.userid == OperateContext.Current.Usr.id || OperateContext.Current.Usr.username=="admin")
                {
                    if (oneModel.downState==1)
                    {
                        string vol = StringHelper.GetNum(oneModel.vol);
                        string num = StringHelper.GetNum(oneModel.num);
                        string imgdir = System.IO.Path.Combine("C:\\wwwroot\\49355\\", $"{oneModel.years}\\{oneModel.gch}\\{vol}\\{num}");
                        string pdfname = $"{oneModel.gch}{oneModel.years}{vol}{num}.pdf";
                        string localpdffile = System.IO.Path.Combine(imgdir, pdfname);  //本地PDF文件
                        byte[] fileBytes = System.IO.File.ReadAllBytes(localpdffile); // get the file data
                        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, pdfname); // return the file

                    }
                    else
                    {
                        return OperateContext.Current.RedirectAjax("err", "下载失败,下载状态不对" , null, "");
                    }

                }
                else
                {

                    return OperateContext.Current.RedirectAjax("err", "非法操作", null, "");
                }
              
            }
            else
            {
                return OperateContext.Current.RedirectAjax("err", "下载错误哦", null, "");
            }
        }

    }
}